mui.init({
	beforeback: function() {
		var parentObj = plus.webview.currentWebview().opener();
		mui.fire(parentObj, 'dataList');
	},
	pullRefresh: {
		container: "#refreshContainer", //下拉刷新容器标识，querySelector能定位的css选择器均可，比如：id、.class等
		down: {
			style: 'circle', //必选，下拉刷新样式，目前支持原生5+ ‘circle’ 样式
			color: '#2BD009', //可选，默认“#2BD009” 下拉刷新控件颜色
			height: '50px', //可选,默认50px.下拉刷新控件的高度,
			range: '100px', //可选 默认100px,控件可下拉拖拽的范围
			offset: '0px', //可选 默认0px,下拉刷新控件的起始位置
			auto: false, //可选,默认false.首次加载自动上拉刷新一次
			callback: function () {
				my_order.get_order_list();
				setTimeout(function() {
					mui('#refreshContainer').pullRefresh().endPulldownToRefresh(false);
				}, 1000);
			}
		}
	}
});

/*document.addEventListener("swipeleft", function () {
	my_order.tabIndex = 'ebook';
	my_order.get_order_list();
});

document.addEventListener("swiperight", function () {
	my_order.tabIndex = 'course';
	my_order.get_order_list();
});*/

document.addEventListener("updateData", function () {
	my_order.get_order_list();
	my_order.getUserLoginSta();
});

mui.plusReady(function() {
	hideScrollBar();
	plus.navigator.setStatusBarStyle('dark');
	my_order = new Vue({
		el: '#my_order',
		data: {
			tabIndex: 'course',
			class_status:0,
			ebook_status:0,
			class_list: [],
			ebook_list:[],
			loginSta:0,
		},
		mounted() {
			this.getUserLoginSta();
			this.get_order_list();
		},
		watch:{
			tabIndex:function (val,oldval) {
				var self = this;
				if(self.loginSta == 0){
					self.class_status = 0;
					self.ebook_status = 0;
				}else{
					if(val == 'course' && self.class_list.length == 0){
						self.get_order_list();
					}else if(val == 'ebook' && self.ebook_list.length == 0){
						self.get_order_list();
					}
				}

			}
		},
		methods: {
			select_tap: function(e) {
				this.tabIndex = e.target.getAttribute('tab-index');
			},
			getUserLoginSta:function(){
				var api_host = plus.storage.getItem('HOST_URL');
				var self = this;
				mui.ajax({
					url: api_host + '/api/linxuan/user/checkLoginStatus',
					dataType: 'json',
					headers:{'User-Token':plus.storage.getItem('UserToken')},
					timeout:3000,
					type: 'get',
					success: function (msg) {
						if (msg.code == 1) {
							self.loginSta = 1;
						} else {
							self.loginSta = 0;
						}
					},
					error: function (xhr, type, errorThrown) {
						if (network > 1) {
							mui.toast('登录状态请求失败请重试', {duration: '300',type:'div'});
						}
					}
				});
			},
			get_order_list: function() {
				showWatting();
				var self = this;
				var api_host = plus.storage.getItem('HOST_URL');
				mui.ajax({
					url: api_host + '/api/user/user/myPaidOrder',
					dataType: 'json',
					data: {
						'order_type': self.tabIndex
					},
					headers: {
						'User-Token': plus.storage.getItem('UserToken')
					},
					timeout: 3000,
					type: 'get',
					success: function(msg) {
						if (msg.code == 1) {
							if(self.tabIndex == 'course'){
								self.class_list.length = 0;
								self.class_list = msg.data;
								self.class_status = 1;
							}else{
								self.ebook_list.length = 0;
								self.ebook_list = msg.data;
								self.ebook_status = 1;
							}
						}
					},
					error: function() {
                        mui.toast('数据获取失败了，请重试', {duration: '2000',type: 'div'});
					},
					complete: function() {
						plus.nativeUI.closeWaiting();
					}
				})
			},

			open_class_list: function(dataID) {
				mui.openWindow({
					url: '../courseDetail/courseDetail.html',
					extras: {
						'cid': dataID
					},
					createNew: false,
					show: {
						autoShow: true,
						anishow: 'pop-in',
						duration: 400
					},
					waiting: {
						autoShow: false,
					}
				});
			},
			open_ebook_detai: function(dataID) {
				//打开电子书详情页面
				mui.openWindow({
					url: '../eBook/eBookDetail.html',
					extras: {
						'bookid': dataID
					},
					createNew: false,
					show: {
						autoShow: true,
						anishow: 'pop-in',
						duration: 400
					},
					waiting: {
						autoShow: false,
					}
				});
			},
			moreCourses: function() {
				mui.openWindow({
					url: '../columns/course.html',
					createNew: false,
					show: {
						autoShow: true,
						anishow: 'pop-in',
						duration: 400
					},
					waiting: {
						autoShow: false,
					}
				})
			},
			moreBooks: function() {
				mui.openWindow({
					url: '../columns/bookCity.html',
					createNew: false,
					show: {
						autoShow: true,
						anishow: 'pop-in',
						duration: 400
					},
					waiting: {
						autoShow: false,
					}
				})
			}
		}
	})
});

mui('#slider').on('tap', '.icon_sr_black', function() {
	mui.openWindow({
		url: '../search/search.html',
		createNew: false,
		show: {
			autoShow: true,
			anishow: 'pop-in',
			duration: 400
		},
		waiting: {
			autoShow: false,
		}
	})
});
